var WIN=(function(){
	var win_maskBG=document.createElement("div");
	var win_content=document.createElement("div");
	var win_content2=document.createElement("div");
	var win_content_top=document.createElement("div");
	var win_content_bom=document.createElement("div");	
	var win_content_top_spanL=document.createElement("span");
	var win_content_top_spanR=document.createElement("span");
	var setting={},tempTop,tempContent;
	var winFlag=false,winpgFlag=false;
	var _setting={
		width:	400,
		height:	300,
		top:	0,
		left:	0,
		_top:	0,
		_left:	0,
		fromObj:	$('win_temp'),
		afterShow:	function(){},
		afterClose:	function(){},
		hide:		function(){},
		celhide:	function(){}
	};
	
	function _init(){
		win_maskBG.id="win_maskBG";
		win_maskBG.className="win_maskBG";
		document.body.appendChild(win_maskBG);

		win_content.id="win_content";
		win_content.className="win_content";
		document.body.appendChild(win_content);	
		win_content_top.id="win_content_top";
		win_content_top.className="win_content_top";
		win_content.appendChild(win_content_top);
		win_content_bom.id="win_content_bom";
		win_content_bom.className="win_content_bom";
		win_content.appendChild(win_content_bom);
		win_content_top_spanL.id="win_content_top_spanL";
		win_content_top_spanL.className="win_content_top_spanL";
		win_content_top.appendChild(win_content_top_spanL);
		win_content_top_spanR.id="win_content_top_spanR";
		win_content_top_spanR.className="win_content_top_spanR";
		win_content_top.appendChild(win_content_top_spanR);
		win_content_top_spanR.innerHTML='<a href="javascript:;" onclick="WIN.close()"><img src="images/close.gif"/></a>';
		
		win_content2.id="win_content2";
		win_content2.className="win_content2";
		document.body.appendChild(win_content2);
		win_content2.innerHTML='<img src="images/loading-1.gif"/><br/>正在进行操作，请稍候 ...';	
	}
	function addListener(el,eventType,fn){
		if(el.addEventListener){el.addEventListener(eventType,fn,false);}
		else if(el.attachEvent){el.attachEvent("on" + eventType,fn);}
		else{el["on"+eventType] = fn;}
	}
	addListener(window,"load",_init);

	function extend(destination,source) { 
		for(var property in source){destination[property] = source[property];} 
		return destination; 
	} 
	
	function setWin(f){
		var maxMaskWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth, document.documentElement.clientWidth);
		var maxMaskHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight);
		setting.top=parseInt((document.documentElement.clientHeight-setting.height)/2+document.documentElement.scrollTop);
		setting.top=(setting.top+setting._top)>2?setting.top:(2-setting._top);		
		setting.left=parseInt((maxMaskWidth-setting.width)/2);
		with(win_maskBG.style){width=maxMaskWidth+'px';height=maxMaskHeight+'px';}
		with(win_content.style){width=setting.width+'px';height=setting.height+'px';}		
		if(f){
			tempTop=-setting.top;
			win_content.style.top=tempTop+'px';
			win_content.style.left=setting.left+setting._left+'px';
			win_maskBG.style.display="block";
			win_content.style.display="block";
			shade();
		}else{
			win_content.style.top=setting.top+setting._top+'px';
			win_content.style.left=setting.left+setting._left+'px';
			win_maskBG.style.display="block";
			win_content.style.display="block";
			setting.afterShow();
		}
	}
	
	function setWin2(){
		var maxMaskWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth, document.documentElement.clientWidth);
		var maxMaskHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight);
		setting.top=parseInt((document.documentElement.clientHeight-setting.height)/2+document.documentElement.scrollTop);
		setting.left=parseInt((maxMaskWidth-setting.width)/2);
		with(win_maskBG.style){width=maxMaskWidth+'px';height=maxMaskHeight+'px';display='block';}
		with(win_content2.style){width=setting.width+'px';height=setting.height+'px';top=setting.top+setting._top+'px';left=setting.left+setting._left+'px';display='block'}
	}
	
	function shade(){
		tempTop=Math.floor(tempTop+(setting.top+setting._top-tempTop)*0.3);
		if(tempTop==parseInt(win_content.style.top)) tempTop=setting.top+setting._top;
		win_content.style.top=tempTop+'px';
		if(tempTop<setting.top+setting._top){setTimeout(shade,20);}else{setting.afterShow();}
	}

		
	return {
		win:	null,
		winBg:	null,
		adjust:function(){
			if(winFlag){setWin(false)};
			if(winpgFlag){setWin2()};
		},
		/*show win*/
		show:function(_title,_contentObj,_sty){
			tempContent=_contentObj.cloneNode(true);
			extend(setting,_setting);
			extend(setting,_sty||{});
			win_content_top_spanL.innerHTML=_title;
			if(win_content_bom.getElementsByTagName("div")[0])
				win_content_bom.removeChild(win_content_bom.getElementsByTagName("div")[0]);
			win_content_bom.appendChild(_contentObj.cloneNode(true));
			win_content_bom.getElementsByTagName("div")[0].style.display="block";
			setWin(true);
			if(setting.fromObj) setting.fromObj.removeChild(_contentObj);
			this.win=win_content;
			this.winBg=win_maskBG;
			winFlag=true;
		},
		hide:function(){
			setting.hide();
			winFlag=false;
		},
		celhide:function(){
			setting.celhide();
			winFlag=true;
		},
		/*close win and deal with other*/
		close:function(){
			win_maskBG.style.display="none";
			win_content.style.display="none";
			if(win_content_bom.getElementsByTagName("div")[0]){
				setting.fromObj.appendChild(tempContent)
				win_content_bom.removeChild(win_content_bom.getElementsByTagName("div")[0]);
			}
			setting.afterClose();
			winFlag=false;
		},
		/*show progress*/
		showpg:function(){
			extend(setting,_setting);
			extend(setting,{width:120,height:30,_top:-130});
			setWin2();
			winpgFlag=true;
		},
		/*hide progress*/
		closepg:function(){
			win_maskBG.style.display="none";
			win_content2.style.display="none";
			winpgFlag=false;
		}
	}
})();